﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SqlLiteTest
{
    class DbfDotNetDatabase : ITestDatabase
    {
        #region ITestDatabase Members

        DbfDotNet.DbfTable<DbfDotNetIndividual> mIndividuals;
        DbfDotNet.DbfIndex<DbfDotNetIndividual> mDobIndex;

        void ITestDatabase.OpenAndClear()
        {
            foreach (var fi in new System.IO.DirectoryInfo(".").GetFiles("*.ndx"))
            {
                fi.Delete();
            }
            foreach (var fi in new System.IO.DirectoryInfo(".").GetFiles("*.dbf"))
            {
                fi.Delete();
            }

        }

        void ITestDatabase.CreateIndividualTable()
        {
            mIndividuals = new DbfDotNet.DbfTable<DbfDotNetIndividual>(@"individuals.dbf", Encoding.ASCII, DbfDotNet.DbfVersion.dBaseIV);
        }

        void ITestDatabase.InsertNewIndividual(int id, string firstname, string middlename, string lastname,
            DateTime dob, string state)
        {
            var indiv = mIndividuals.NewRecord();
            indiv.FIRSTNAME = firstname;
            indiv.MIDDLENAME = middlename;
            indiv.LASTNAME = lastname;
            indiv.DOB = dob;
            indiv.STATE = state;
            indiv.SaveChanges();
        }

        IIndividual ITestDatabase.GetIndividualById(int id)
        {
            DbfDotNetIndividual result = mIndividuals.GetRecord((UInt32)id);
            return result;
        }

        void ITestDatabase.SaveIndividual(IIndividual individual)
        {
            ((DbfDotNetIndividual)individual).SaveChanges();
        }

        void ITestDatabase.Close()
        {
            mIndividuals.Close();
        }

        void ITestDatabase.BeginUpdates()
        {
        }

        void ITestDatabase.EndUpdates()
        {
        }

        #endregion

        #region ITestDatabase Members


        void ITestDatabase.CreateDobIndex()
        {
            var sortOrder = new DbfDotNet.SortOrder<DbfDotNetIndividual>(/*unique*/false);
            sortOrder.AddField("DOB");
            mDobIndex = mIndividuals.GetIndex("DOB.NDX", sortOrder);
        }

        IEnumerable<IIndividual> ITestDatabase.IndividualsByAge()
        {
            foreach (DbfDotNetIndividual indiv in mDobIndex)
            {
                yield return indiv;
            }
        }

        #endregion
    }

#pragma warning disable 649

    class DbfDotNetIndividual : DbfDotNet.DbfRecord, IIndividual
    {
        //public int ID;
        //[DbfDotNet.Column(Width = 20)]
        public string FIRSTNAME;
        [DbfDotNet.Column(Width = 20)]
        public string MIDDLENAME;
        [DbfDotNet.Column(Width = 20)]
        public string LASTNAME;
        public DateTime DOB;
        [DbfDotNet.Column(Width = 20)]
        public string STATE;

        #region IIndividual Members

        int IIndividual.ID
        {
            get { return (int)base.RecordNo; }
            set { throw new Exception("RecordNo is readonly in DbfDotNet"); }
        }

        string IIndividual.FirstName
        {
            get { return FIRSTNAME; }
            set { FIRSTNAME = value; }
        }

        string IIndividual.MiddleName
        {
            get { return MIDDLENAME; }
            set { MIDDLENAME = value; }
        }

        string IIndividual.LastName
        {
            get { return LASTNAME; }
            set { LASTNAME = value; }
        }

        DateTime IIndividual.Dob
        {
            get { return DOB; }
            set { DOB = value; }
        }

        string IIndividual.State
        {
            get { return STATE; }
            set { STATE = value; }
        }

        #endregion
    }

}
